#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/*
Using hardware:                   PWM
Number of channels:                 8
PWM frequency:                 100 Hz
PWM steps:                       1000
Maximum period (100  %):      10000us
Minimum period (0.100%):         10us
*/

//const char* deviceFile= "/dev/pi-blaster";
const char* deviceFile= "pi-blaster_debug.txt";

/*Hilfsroutinen*/
int writeFile(char* s)
{
	FILE *fp;
	fp= fopen(deviceFile, "w+");
	if (fp == NULL)
		printf("Hat leider nicht geklappt\n");
	else
		printf("Versuche zu schreiben\n");
	fprintf(fp, s);
	fclose(fp);
}

/*Hauptroutine*/
int main()
{
    float min=0.05; //1ms
    float max=0.10; //2ms
    float currentStep;
    int currentServo=1;
    float stepWidth;
    int i;
    int nSteps= 10;
	char buffer[100];
	char key;
	
	for (;;){
	printf("Type in a key: ");
	key = fgetc(stdin);
	printf("\nThe key was %c\n", key);
	}
	
	stepWidth= (max-min)/nSteps;
    currentStep= min;
    //printf("Starte Servodrehung:\n");
    for(i=0; i<nSteps; i++)
    {
        currentStep= currentStep + stepWidth;
        snprintf(buffer, sizeof (buffer), "%d=%f\n", currentServo, currentStep);  //"0=0.05"
		printf(buffer);
		writeFile(buffer);
        usleep(1000000);	//1 sek
    }
    //printf("Servodrehung fertig!\n");
    return 0;
}
